<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2016/11/7
 * Time: 10:15
 */
header('Content-Type:text/html;charset=utf-8');
echo '<h2>接收到新用户注册！</h2>';
echo '<p>用户名：'.$_POST['username'].'</p>';
echo '<p>密码：'.$_POST['password'].'</p>';
echo '<p>邮箱：'.$_POST['email'].'</p>';
echo '<p>确认密码：'.$_POST['password'].'</p>';
echo '<p>IP地址：'.$_SERVER['REMOTE_ADDR'].'</p>';
echo '<p>浏览器环境：'.$_SERVER['HTTP_USER_AGENT'].'</p>';
echo '<p>请求来源：'.$_SERVER['HTTP_REFERER'].'</p>';

$username = $_POST['username'];
$password = $_POST['password'];
$email = $_POST['email'];
$link = mysqli_connect('localhost','root','','itcast');
if(!$link) {
    die('链接数据库失败' . mysqli_error($link));
}
mysqli_query($link,'set names utf8');
$username = mysqli_real_escape_string($link,$username);
$email = mysqli_real_escape_string($link,$email);
$sql = "select `id` from`user` WHERE `username`= `$username` ";
//$rst = mysqli_query($link,$sql);
if (mysqli_fetch_row(mysqli_query($link,$sql))){
    die('用户名已经存在，请换个用户名.');
}
$password = md5($password);
$sql = "insert into`user` (`username`,`password`,`email`) values 
                          ('$username','$password','$email')";
$rst = mysqli_query($link,$sql);

require 'check_form.lib.php';
$data = array(
    'username'=>'小明';
    'password'=>'123456';
    'email'=>'xiaoming@123.com',
);
$validata = array(
    'username'=>'checkusername',
    'password'=>'checkPassword',
    'email'=>'checkEmail',
);
$error = array();
foreach ($validata as $k=>$v){
    $result = $v($data[$k]);
    if($result !== true){
        $error[]= $result;
    }
    if(empty($error)){
    }else{
        require 'register_error_html.html';
    }
}
